package com.rimi.controller;

import com.rimi.bean.*;
import com.rimi.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


@Controller
@RequestMapping("/listpage")
public class ListPageController {

    @Autowired
    private MenuTypeService menuTypeService;
    @Autowired
    private MenuAddressService menuAddressService;
    @Autowired
    private MenuTimeService menuTimeService;
    @Autowired
    private MenuService menuService;
    @Autowired
    private VideoService videoService;


    //把菜单列表发送到list_page
    @RequestMapping("index")
    public String getMenuList(Model model, HttpServletRequest request){

        String menu_name = request.getParameter("menu_name");
        String nowPage = request.getParameter("nowPage");

        String videoName = request.getParameter("videoName");
        model.addAttribute("videoName", videoName);
        //通过主菜单名获取到查询条数
        int count1 = videoService.selectCount1(menu_name);
        Page page=new Page();
        //获取尾页
        page.setCount(count1);
        int totalPage = page.getTotalPage();
        int now1=0;
        if (nowPage!=null&&!"".equals(nowPage)){
            System.out.println("1");
            now1=Integer.parseInt(nowPage);
            if (now1<1){
                now1=1;
            }
            if (now1>totalPage){
                now1=totalPage;
            }
            page.setNowPage(now1);
        }
        int a=page.getNowPage()-1;
        if (a<0){
            a=a*-1;
        }
        int num1=a*page.getPageSize();
        //通过参数展示分类的视频列表
        List<Video> list = videoService.selectLike1(menu_name, num1);
        model.addAttribute("VideoByMenuName", list);
        model.addAttribute("page", page);

        //展示类型菜单
        List<MenuType> menuTypeByMenuName = menuTypeService.getMenuTypeByMenuName(menu_name);
        model.addAttribute("MenuType", menuTypeByMenuName);
        //展示地址菜单
        List<MenuAddress> menuAddressList = menuAddressService.getMenuAddressByMenuName(menu_name);
        model.addAttribute("MenuAddress", menuAddressList);
        //展示时间菜单
        List<MenuTime> menuTimeByMenuName = menuTimeService.getMenuTimeByMenuName(menu_name);
        model.addAttribute("MenuTime", menuTimeByMenuName);
        //展示主菜单
        List<Menu> menuList = menuService.getMenuList();
        model.addAttribute("Menu", menuList);
        //展示主菜单的相关视频
        List<Video> videoByVideoMenu = videoService.getVideoByVideoMenu(menu_name);
        model.addAttribute("VideoImage", videoByVideoMenu);
        //将menuName存入
        model.addAttribute("menuName", menu_name);


        return "list_page";
    }


}
